package com.huifaapp.mall.selectImage;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.BaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PhotoalbumActivity extends BaseActivity implements
		OnItemClickListener {
	Map<String, List<String>> AlbumMap;
	List<String> paths;
	List<String> pathList;
	ListView listView;
	LruCache<String, Bitmap> cache;
	PhotoalbumAdapetr photoalbumAdapetr = new PhotoalbumAdapetr();
	PhotoalbumAdapetr adapetr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photoalbum);
		try {
			initIntent();
			initAlbumMap();
			init();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void initIntent() {
		Intent intent = getIntent();
		if (intent == null)
			return;
		Bundle bundle = intent.getExtras();
		if (bundle == null)
			return;
		paths = bundle.getStringArrayList("paths");
	}

	public void init() {
		listView = (ListView) findViewById(R.id.lvPhotoalbum);
		adapetr = new PhotoalbumAdapetr();
		cache = application.map;
		listView.setAdapter(adapetr);
		listView.setOnItemClickListener(this);
	}

	public void initAlbumMap() {
		if (paths == null)
			return;
		AlbumMap = new HashMap<String, List<String>>();
		pathList = new ArrayList<String>();
		pathList.add("所有图片");
		AlbumMap.put("所有图片", paths);
		for (String path : paths) {
			int index = path.lastIndexOf("/");
			int index2 = path.lastIndexOf("/", index - 1);
			String p = path.substring(index2, index);
			boolean isAdd = true;
			for (String key : AlbumMap.keySet()) {
				if (key.equals(p)) {
					isAdd = false;
					AlbumMap.get(key).add(path);
					break;
				}
			}
			if (isAdd) {
				pathList.add(p);
				List<String> MapPahts = new ArrayList<String>();
				MapPahts.add(path);
				AlbumMap.put(p, MapPahts);
			}
		}
	}

	public void StartClass() {

		Intent intent = new Intent();

		setResult(RESCODE_STRING_CLOSE, intent);
		finish();
	}

	public void onAction(View v) {
		switch (v.getId()) {
			case R.id.main_back:
				StartClass();
				break;
		}
	}

	class PhotoalbumAdapetr extends BaseAdapter {

		@Override
		public int getCount() {
			if (pathList == null)
				return 0;
			return pathList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return pathList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			if (arg1 == null) {
				arg1 = LayoutInflater.from(PhotoalbumActivity.this).inflate(R.layout.item_photoalbumlist, null);
			}
			String path = pathList.get(arg0);
			TextView textView = (TextView) arg1.findViewById(R.id.tvPathName);
			ImageView imageView = (ImageView) arg1.findViewById(R.id.ivTypical);
			int pathCount = AlbumMap.get(path).size();
			textView.setText(path + "(" + pathCount + ")");
			if (cache.get(AlbumMap.get(path).get(0)) != null) {
				imageView.setImageBitmap(cache.get(AlbumMap.get(path).get(0)));
			} else {
				LocalImageThread imageThread = new LocalImageThread(AlbumMap.get(path).get(0), 100, 100, imageView);
				imageThread.setIlocalImageAsyncTask(asyncTaskListener);
				imageThread.start();
			}
			return arg1;
		}
	}

	LocalImageThread.ILocalImageAsyncTask asyncTaskListener = new LocalImageThread.ILocalImageAsyncTask() {
		@Override
		public void UIUpdate(String path, Bitmap bitmap, ImageView imageView) {
			// TODO Auto-generated method stub
			cache.put(path, bitmap);
			imageView.setImageBitmap(bitmap);
		}
	};

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			StartClass();
		}
		return super.onKeyDown(keyCode, event);
	};

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		String key = pathList.get(arg2);
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putStringArrayList("paths", (ArrayList<String>) AlbumMap.get(key));
		intent.putExtras(bundle);
		setResult(RESCODE_STRING, intent);
		finish();
	}

	public static final int RESCODE_STRING = 5;
	public static final int RESCODE_STRING_CLOSE=6;

	@Override
	protected int getLayoutId() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	protected void initialize() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub

	}
}
